﻿/*  
 *  IIS LogFile Analyser, Copyright (C) 2013. http://www.iis-analyser.com
 *  --
 *  This program is free software, provided "AS IS" WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
 * 
 *  File Author: Ian Quigley,    2013-02-23
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace DataStore
{
    [TableName("Site")]
    public class SiteRecord
    {
        [FieldDescription("Id", "Long, primary key")]
        public long Id {  get; private set; }
        [FieldDescription("Name", "string")]
        public string Name { get; set; }
        [FieldDescription("Folder", "string, path of log files")]
        public string Folder { get; set; }
                
        internal SiteRecord()
        {}

        internal static void InitialiseTable(  )
        {
            using (SQLiteConnection connection = new SQLiteConnection( Database.DataSource ))
            using (SQLiteCommand command = new SQLiteCommand( "CREATE TABLE if not exists Site (Id Integer PRIMARY KEY AutoIncrement, Name Text, Folder text)", connection ))
            {
                connection.Open();
                command.ExecuteNonQuery();
                connection.Close();
            }
        }

        internal void Save()
        {
            using (SQLiteConnection connection = new SQLiteConnection( Database.DataSource ))
            using (SQLiteCommand command = new SQLiteCommand( "INSERT OR REPLACE INTO Site (" + (Id == 0 ? "" : "Id, ") + "Name, Folder) VALUES (" + (Id == 0 ? "" : "@Id, ") + "@name, @folder); select last_insert_rowid();", connection ))
            {
                if (Id != 0)
                    command.Parameters.AddWithValue( "@Id", Id);

                command.Parameters.AddWithValue( "@name", Name );
                command.Parameters.AddWithValue( "@folder", Folder);
                connection.Open();
                Id = (long)command.ExecuteScalar();
                connection.Close();
            }
        }

        internal static SiteRecord Read( long siteId )
        {
            using (SQLiteConnection connection = new SQLiteConnection( Database.DataSource ))
            using (SQLiteCommand command = new SQLiteCommand( "SELECT Id, Name, Folder FROM Site WHERE Id = @p0;", connection ))
            {
                command.Parameters.AddWithValue( "@p0", siteId);
                connection.Open();
                var reader = command.ExecuteReader();

                if (!reader.Read())
                    return null;

                return FromReader(reader);
            }
        }

        internal static SiteRecord FindOrCreate( string siteName, string folder )
        {
            using (SQLiteConnection connection = new SQLiteConnection( Database.DataSource ))
            using (SQLiteCommand command = new SQLiteCommand( "SELECT Id, Name, Folder FROM Site WHERE Name like @p0;", connection ))
            {
                command.Parameters.AddWithValue( "@p0", siteName );
                connection.Open();
                var reader = command.ExecuteReader();

                SiteRecord site = new SiteRecord( );
                if (!reader.Read())
                {
                    connection.Close();
                    site.Name = siteName;
                    site.Folder = folder;
                    site.Save();
                }
                else
                {
                    site.Id = (long)reader[0];
                    site.Name = (string)reader[1];
                    site.Folder = (string)reader[2];
                }
                connection.Close();
                return site;
            }
        }

        internal static IEnumerable<SiteRecord> ReadAll()
        {
            using (SQLiteConnection connection = new SQLiteConnection( Database.DataSource ))
            using (SQLiteCommand command = new SQLiteCommand( "SELECT Id, Name, Folder FROM Site;", connection ))
            {
                connection.Open();
                var reader = command.ExecuteReader();
                while (reader.Read())
                    yield return SiteRecord.FromReader( reader );
            }            
        }

        private static SiteRecord FromReader( SQLiteDataReader reader )
        {
            return new SiteRecord()
               {
                Id = (long)reader["Id"],
                Name = (string)reader["Name"],
                Folder = (string)reader["Folder"]
                };
        }

        internal static void Delete( int id )
        {
            using (SQLiteConnection connection = new SQLiteConnection( Database.DataSource ))
            using (SQLiteCommand command = new SQLiteCommand( "DELETE FROM Site WHERE Id = @p0;", connection ))
                {
                    connection.Open();
                    command.Parameters.AddWithValue( "@p0", id );
                    command.ExecuteNonQuery();
            }
        }

        public IEnumerable<FileRecord> Files
        {
            get
            {
                return FileRecord.ForSite( Id );
            }
        }

        
    }
}
